package com.cwt_taxi_xa.protocol.mcu;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Handler;
import android.widget.Toast;

import com.cwt_taxi_xa.ActivityFM;
import com.cwt_taxi_xa.MainApplication;
import com.cwt_taxi_xa.protocol.MCUUtil;
import com.cwt_taxi_xa.util.AppBiz;
import com.cwt_taxi_xa.util.AppLog;
import com.cwt_taxi_xa.util.Tools;

import java.lang.ref.WeakReference;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created by undi on 2019/7/13 15:48
 */
public class PartyControl {
    private static final String TAG = "PartyControl";

    private Handler mHandler = new Handler();

    private WeakReference<IViewFmManager> weakReference;

    public void decode(byte[] buffer){
        AppLog.e(TAG,"方控结果：" + Tools.parseByte2HexStr(buffer));
        if(buffer.length >= 1){
            switch (buffer[0]){
                case 1:
                    AppLog.e(TAG,"方控结果：" + Tools.parseByte2HexStr(buffer));
                    MCUUtil.setvolAdd();
                    setVolAdd();
                    break;
                case 2:
                    AppLog.e(TAG,"方控结果：" + Tools.parseByte2HexStr(buffer));
                    MCUUtil.setvolLess();
                    setVolLess();
                    break;
                case 3:
                    previous();
                    break;
                case 4:
                    next();
                    break;
                case 5:
                    onChangeMode();
                    break;
                default:
                    break;
            }
        }
    }


    public void register(IViewFmManager viewFmManager){
        weakReference = new WeakReference<>(viewFmManager);
    }

    public void unregister(IViewFmManager viewFmManager){
        weakReference.clear();
    }

    private void previous(){
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                if(weakReference != null){
                    IViewFmManager viewFmManager = weakReference.get();
                    if(viewFmManager != null){
                        viewFmManager.previous();
                    }
                }
            }
        });
    }

    private void next(){
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                if(weakReference != null){
                    IViewFmManager viewFmManager = weakReference.get();
                    if(viewFmManager != null){
                        viewFmManager.next();
                    }
                }
            }
        });
    }

    private void setVolAdd(){
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                if(weakReference != null){
                    IViewFmManager viewFmManager = weakReference.get();
                    if(viewFmManager != null){
                        viewFmManager.setVolAdd();
                    }else{
                        MCUUtil.setvolAdd();
                    }
                }
            }
        });
    }

    private void setVolLess(){
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                if(weakReference != null){
                    IViewFmManager viewFmManager = weakReference.get();
                    if(viewFmManager != null){
                        viewFmManager.setVolLess();
                    }else{
                        MCUUtil.setvolLess();
                    }
                }
            }
        });
    }

    private void closeFm(){
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                if(weakReference != null){
                    IViewFmManager viewFmManager = weakReference.get();
                    if(viewFmManager != null){
                        viewFmManager.finish();
                    }
                }
            }
        });
    }

    private final int modeCount = 3;
    private int currentMode = 1;
    public void onChangeMode(){
        if(++currentMode > modeCount){
            currentMode = 1;
        }
        switch(currentMode){
            case 1:
                startApp("com.autonavi.amapauto");//启动导航
                break;
            case 2:
                startActivity(ActivityFM.class);//启动收音
                forceStopPackage("com.android.music",MainApplication.getInstance());
                break;
            case 3:
                //startAux();
                startMusic();
                closeFm();
                break;
            case 4:
                //startMusic();
                break;
        }
    }

    private void startAux(){
        MCUUtil.selecterVic(3);
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(MainApplication.getInstance(), "AUX声道", Toast.LENGTH_LONG).show();
            }
        });
    }

    private void startActivity(Class<?> cls){
            try {
                Intent intent = new Intent(MainApplication.getInstance(), cls);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                MainApplication.getInstance().startActivity(intent);
            } catch (Exception e) {
                e.printStackTrace();
            }
    }


    private void forceStopPackage(String pkgName, Context context){
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        try {
            Method method = am.getClass().getMethod("forceStopPackage", String.class);
            method.invoke(am, pkgName);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }

    private void startApp(String packageName){
        new AppBiz(MainApplication.getInstance()).launchApp(packageName);
    }

    private void startMusic(){
        if(checkInstall(MainApplication.getInstance(),"com.android.music")){
            new AppBiz(MainApplication.getInstance()).launchApp("com.android.music");
        }else{
            MCUUtil.InsMusic(MainApplication.getInstance());
        }
    }

    public boolean checkInstall(Context context, String pkgName) {
        // TODO Auto-generated method stub
        boolean install=false;
        try {
            PackageInfo info = context.getPackageManager().getPackageInfo(pkgName,PackageManager.GET_ACTIVITIES);
            if (info!=null&&info.activities.length>0) {
                install=true;
            }
        } catch (PackageManager.NameNotFoundException e) {
            // TODO Auto-generated catch block
//			e.printStackTrace();
//			AppLog.e(ExceptionUtil.getInfo(e));
        }
        AppLog.i( "包名: "+pkgName+"; 是否安装: " + install);
        return install;
    }

    //导航
    //FM
    //AUX
    //音乐
}
